package views.mediators
{
	import com.axon.base.helpers.AxFailure;
	import com.axon.base.managers.AxErrorManager;
	import com.axon.components.controls.AxDataGrid;
	import com.axon.mvc.helpers.AxVerbs;
	import com.axon.mvc.patterns.mediator.AxMediator;
	
	import flash.display.DisplayObject;
	
	import helpers.Constants;
	import helpers.ListaPleitos;
	import helpers.Verbs;
	
	import models.vo.Pleito;
	import models.vo.User;
	
	import mx.containers.TabNavigator;
	
	import views.components.PleitoListaTriagemView;

	public class PleitoListaTriagemMediator extends AxMediator
	{
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		public function PleitoListaTriagemMediator(viewComponent: DisplayObject) { super(viewComponent); }
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		public function get view(): PleitoListaTriagemView { return viewComponent as PleitoListaTriagemView; }
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		override protected function initialize(): void 
		{
			regBroadcastInterests( [ AxVerbs.REFRESH ] );
			 
			regResponseInterest( Pleito, [	AxVerbs.LIST,
			 								Verbs.REGISTRAR_PROTOCOLO,
			 								Verbs.ACEITAR_INSCRICAO,
			 								Verbs.REJEITAR_INSCRICAO
			 							  ] );
		}	
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		public function eventViewShow(): void
		{
			view.adgMain.dataProvider = null;
			view.adgMain.setFocus();
			callController( Pleito, AxVerbs.LIST, Constants.MNEMONICO_LISTA_TRIAGEM );
		}
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		public function eventEnableGestures(): void
		{
    		view.pbAbrir.enabled    			=  ( view.selectedPleito != null );
    		                   	  
    		view.pbRegistrarProtocolo.enabled	=  ( view.pbAbrir.enabled )
    		     	              	  			&& ( AppFacade.getInstance().sessionProxy.user.hasRole(User.ROLE_CONFERENTE))

    		view.pbRegistrarDocumentos.enabled	=  ( view.pbRegistrarProtocolo.enabled )
    											&& ( view.selectedPleito.status == Constants.STATUS_PLEITO_TRIAGEM );

    		view.pbAceitar.enabled				=  ( view.pbRegistrarDocumentos.enabled );

    		view.pbRejeitar.enabled				=  ( view.pbRegistrarDocumentos.enabled );
		}

		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		public function brcastRefresh(sender: Object, body: Object, extraInfo: Object): void
		{
			AppFacade(facade).pleitoProxy.listaTriagem.invalidate();

			if  ( (view.parent is TabNavigator) && ((view.parent as TabNavigator).selectedChild == view) ) 
				eventViewShow();
		}
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		public function respPleitoListSuccess( sender: Object, body: Object, extraInfo: Object ): void
		{
			if ( body == AppFacade(facade).pleitoProxy.listaTriagem ) {
				view.adgMain.dataProvider = (body as ListaPleitos).modelsCollection;
				view.adgMain.axRecallLastSelection(); }
		}
		
		public function respPleitoListFailure( sender: Object, body: Object, extraInfo: Object ): void
		{
			if ( body == AppFacade(facade).pleitoProxy.listaTriagem )
				AxErrorManager.showFailure( new AxFailure("Falha no carregamento da lista de pleitos em triagem.") );
		}
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		public function respPleitoRegistrarProtocoloSuccess( sender: Object, body: Object, extraInfo: Object ): void
		{
		    view.adgMain.axReplaceSelectedItem(body);
		}
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		public function respPleitoRegistrarDoctosSuccess( sender: Object, body: Object, extraInfo: Object ): void
		{
		    view.selectedPleito = body as Pleito;
		    eventEnableGestures();
		}

		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		public function respPleitoAceitarInscricaoSuccess( sender: Object, body: Object, extraInfo: Object ): void
		{
		    AxDataGrid(view.adgMain).axRemoveItem(view.selectedPleito);
		}
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		public function respPleitoRejeitarInscricaoSuccess( sender: Object, body: Object, extraInfo: Object ): void
		{
		    AxDataGrid(view.adgMain).axRemoveItem(view.selectedPleito);
		}
		
	}
}